package com.zp.util;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import net.sf.jmimemagic.Magic;
import net.sf.jmimemagic.MagicException;
import net.sf.jmimemagic.MagicMatch;
import net.sf.jmimemagic.MagicMatchNotFoundException;
import net.sf.jmimemagic.MagicParseException;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageUtil {

	/**
	 * compress image
	 * 
	 * @param oldFile
	 *            the path of original image
	 * @param width
	 *            compress width
	 * @param height
	 *            compress height
	 * @param percentage
	 *            whether perceptual if true the width and height will adjust
	 *            automatically
	 * 
	 * @return String the path of file
	 * @throws Exception
	 *             IOException
	 */
	public static String compressImg(String oldFile, int widthdist,
			int heightdist, boolean percentage) {
		String path = null;
		BufferedImage bufferImage = null;
		try {
			File srcfile = new File(oldFile);
			if (srcfile.exists()) {
				Image source = ImageIO.read(srcfile);
				// set the extension for the target file
				String postfix = oldFile.substring(oldFile.indexOf("."));
				String newFile = Constant.PARENT_PATH
						+ Constant.COMP_IMAGE_SAVE_PATH
						+ FileUtil.getInstance().getFileName(postfix);
				if (percentage) {
					// compute the perceptual width and height
					double rate1 = ((double) source.getWidth(null))
							/ (double) widthdist + 0.1;
					double rate2 = ((double) source.getHeight(null))
							/ (double) heightdist + 0.1;
					double rate = rate1 > rate2 ? rate1 : rate2;
					int target_width = (int) (((double) source.getWidth(null)) / rate);
					int target_height = (int) (((double) source.getHeight(null)) / rate);
					// set the image width and height
					bufferImage = new BufferedImage(target_width,
							target_height, BufferedImage.TYPE_INT_RGB);
					bufferImage.getGraphics().drawImage(
							source.getScaledInstance(target_width,
									target_height, Image.SCALE_AREA_AVERAGING),
							Constant.ZERO, Constant.ZERO, null);

				} else {
					bufferImage = new BufferedImage(widthdist, heightdist,
							BufferedImage.TYPE_INT_RGB);
					bufferImage.getGraphics().drawImage(
							source.getScaledInstance(widthdist, heightdist,
									Image.SCALE_AREA_AVERAGING), Constant.ZERO,
							Constant.ZERO, null);

				}
				FileOutputStream out = new FileOutputStream(newFile);
				JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
				encoder.encode(bufferImage);
				out.close();
				path = newFile;
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}

		return path;
	}

	/**
	 * get the type of file
	 * 
	 * @param file
	 * @return String the type of file
	 */
	public static String getType(File file) {
		String type = null;
		MagicMatch match;
		try {
			match = Magic.getMagicMatch(file, true);
			type = match.getMimeType();
		} catch (MagicParseException e) {
			e.printStackTrace();
		} catch (MagicMatchNotFoundException e) {
			e.printStackTrace();
		} catch (MagicException e) {
			e.printStackTrace();
		}
		return type;
	}

	/**
	 * filter the file to accept only accept JPEG,GIF,BMP,PNG
	 * 
	 * @param file
	 * @return if accept return true otherwise false
	 */
	public static boolean acceptType(File file) {
		String type = getType(file);
		if (type.equals(Constant.BMP_TYPE) || type.equals(Constant.GIF_TYPE)
				|| type.equals(Constant.JPEG_TYPE)
				|| type.equals(Constant.PNG_TYPE)) {
			return true;
		} else {
			return false;
		}
	}

}
